<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekend :list="weekendList"></home-weekend> 
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      //city: '',
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json')
        .then(this.getHomeInfoSucc)//axios.get之后返回一个getHomeInfoSucc
    },
    getHomeInfoSucc (res) {
      res = res.data//res是那个index.json，里面有很多属性，这里只要data属性
      if(res.ret && res.data){//判断ret是否为true和是否有数据
        const data = res.data//只要res的data项，不需要ret了
        //this.city = data.city//把json文件中的city数据赋值给这个父组件中的city
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    this.getHomeInfo()//生命周期函数mounted之后，执行getHomeInfo
    this.lastCity=this.city//页面的city
  },
  activated () {
    if(this.lastCity !== this.city){//切换城市后判断是否和之前的城市（lastCity）相同
      this.lastCity = this.city//不想等的话，让lastCity=city，为了下次切换的判断
      this.getHomeInfo()
    }
  }
}
</script>
<style scoped>
</style>
